package uit.comstore.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import uit.comstore.model.blo.CategoryBLO;
import uit.comstore.model.blo.ManufacturerBLO;
import uit.comstore.model.blo.ProductBLO;
import uit.comstore.model.blo.ProductUnitBLO;
import uit.comstore.model.entity.Category;
import uit.comstore.model.entity.Manufacturer;
import uit.comstore.model.entity.Product;
import uit.comstore.model.entity.ProductUnit;

public class ProductAction {
	private ProductBLO productBLO;
	private ProductUnitBLO productUnitBLO;
	private ManufacturerBLO manufacturerBLO;
	private CategoryBLO categoryBLO;
	private List<Product> productList;
	private Product product;
	private List<ProductUnit> unitList;
	private List<Manufacturer> manufacturerList;
	private List<Category> categoryList;
	private ProductUnit unitSelect;
	private Manufacturer manufacturerSelect;
	private Category categorySelect;
	private boolean status;
	private Long id;
	private String brand;
	
	public String getBrand() {
		
		return brand;
	}


	public void setBrand(String brand) {
		this.brand = brand;
	}

	private File file;

	public ProductAction(){
		productBLO = new ProductBLO();
		productUnitBLO = new ProductUnitBLO();	
		manufacturerBLO= new ManufacturerBLO();
		categoryBLO = new CategoryBLO();			
	}

	public String execute(){
		this.productList = productBLO.list();
		this.unitList = productUnitBLO.list();
		this.manufacturerList = manufacturerBLO.list();
		this.categoryList = categoryBLO.list();
		return "success";
	}

	public String add(){
		try{		
			Product product = getProduct();	
			product.setDeleted(0);
			product.setQuantity(0);
			product.setDateAdded(new Date());
			product.setLastModified(new Date());
			manufacturerSelect = manufacturerBLO.getById(product.getManufacturer().getId());
			product.setManufacturer(manufacturerSelect);
			categorySelect = categoryBLO.getById(product.getCategory().getId());
			product.setCategory(categorySelect);
			unitSelect = productUnitBLO.getById(product.getUnit().getId());
			product.setUnit(unitSelect);
			if(product.getImage().isEmpty()){
				product.setImage("default.png");
			}
			if(getStatus()){
				product.setStatus(1);
			}else{
				product.setStatus(0);
			}
			productBLO.add(product);			
		}catch(Exception ex){
			System.out.println("Cannot add Product!");
		}
		return "success";
	}

	public String delete() {
		productBLO.delete(getId());
		return "success";
	}

	public String edit() {
		try{
			Product product = getProduct();
			manufacturerSelect = manufacturerBLO.getById(product.getManufacturer().getId());
			product.setManufacturer(manufacturerSelect);
			categorySelect = categoryBLO.getById(product.getCategory().getId());
			product.setCategory(categorySelect);
			unitSelect = productUnitBLO.getById(product.getUnit().getId());
			product.setUnit(unitSelect);
			/*if(getStatus()){
				product.setStatus(1);
			}else{
				product.setStatus(0);
			}*/
			//if(product.getImage().isEmpty() || product.getImage()==null){
				//product.setImage("default.png");
			//}
			productBLO.edit(product);
		}catch(Exception ex){
			//System.out.println("Cannot add Customer!");
		}
		return "success";
	}

	public void setProductList(List<Product> productList){
		this.productList = productList;
	}

	public List<Product> getProductList(){
		return this.productList;
	}

	public void setProduct(Product product){
		this.product = product;
	}

	public Product getProduct(){
		return this.product;
	}

	public List<ProductUnit> getUnitList() {
		return unitList;
	}

	public void setUnitList(List<ProductUnit> unitList) {
		this.unitList = unitList;
	}

	public List<Manufacturer> getManufacturerList() {
		return manufacturerList;
	}

	public void setManufacturerList(List<Manufacturer> manufacturerList) {
		this.manufacturerList = manufacturerList;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public ProductUnit getUnitSelect() {
		return unitSelect;
	}

	public void setUnitSelect(ProductUnit unitSelect) {
		this.unitSelect = unitSelect;
	}

	public Manufacturer getManufacturerSelect() {
		return manufacturerSelect;
	}

	public void setManufacturerSelect(Manufacturer manufacturerSelect) {
		this.manufacturerSelect = manufacturerSelect;
	}

	public Category getCategorySelect() {
		return categorySelect;
	}

	public void setCategorySelect(Category categorySelect) {
		this.categorySelect = categorySelect;
	}

	public boolean getStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public String lineProducts(){
		return "success";
	}
	
	public String boxProducts(){
		return "success";
	}
	public String showDetail(){
		this.product = productBLO.getById(getId());
		brand = product.getManufacturer().getName();
		return "success";
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

}
